package org.dizem.groupon.action;


import com.opensymphony.xwork2.ActionSupport;
import org.dizem.groupon.model.domain.ImageEntity;
import org.dizem.groupon.service.ImageService;
import org.hibernate.Hibernate;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * User: dizem@126.com
 * Time: 11-6-26 下午11:14
 */
public class UploadImageAction extends ActionSupport {
	private File[] upload;
	private String[] uploadContentType;
	private String[] uploadFileName;
	private ImageService imageService;


	public ImageService getImageService() {
		return imageService;
	}

	public void setImageService(ImageService imageService) {
		this.imageService = imageService;
	}

	public File[] getUpload() {
		return upload;
	}

	public void setUpload(File[] upload) {
		this.upload = upload;
	}

	public String[] getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String[] getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	@Override
	public String execute() throws Exception {
		for (int i = 0; i < uploadFileName.length; ++i) {
			try {
				ImageEntity img = new ImageEntity();
				FileInputStream str = new FileInputStream(upload[i]);
				img.setImage(Hibernate.createBlob(str));
				imageService.save(img);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}
}